package com.andova.component.config;

import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.andova.component.AndovaComponentManager;
import com.andova.toolkit.adapter.BaseAdapter;

import androidx.annotation.NonNull;

import static com.andova.component.config.ConfigAdapter.DEFAULT_SPINNER_STRING;

/**
 * Created by Administrator on 2018-08-21.
 *
 * @author kzaxil
 * @since 0.0.2
 */
class SpinnerAdapter extends BaseAdapter<Value> implements AdapterView.OnItemSelectedListener {
    private String mNode;

    SpinnerAdapter(@NonNull String node) {
        mNode = node;
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        Value object = (Value) parent.getItemAtPosition(position);
        if (object.value.equals(DEFAULT_SPINNER_STRING)) return;
        AndovaComponentManager.getInstance().configComponent().updateValueOfNode(mNode, object);
        ConfigActivity.sHandler.sendEmptyMessage(0);
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    @Override
    public int layout() {
        return R.layout.item_component_textview;
    }

    @Override
    public void execute(View itemView, int position, Value model) {
        TextView rootView = (TextView) itemView.getRootView();
        rootView.setText(model.value);
    }
}
